require('../css/mine-edit.less');
document.ready(function () {
    // 返回
    document.querySelector('.return').addEventListener('click', function () {
        window.history.back();
    })
    // 性别
    const sexPicker = document.querySelector('#sexPicker');
    sexPicker.addEventListener('click', function () {
        weui.picker([{
            label: '男',
            value: 0
        }, {
            label: '女',
            value: 1
        }], {
            onConfirm: function (result) {
                sexText.textContent = result[0].label
            },
            title: '单列选择器'
        });
    });
    // 生日
    const getDay = document.querySelector('#getDay');
    getDay.addEventListener('click', function () {
        weui.datePicker({
            start: 1990,
            end: new Date().getFullYear(),
            onConfirm: function (result) {
                // console.log(result);
                //添零
                let m = result[1].label.substring(0, result[1].label.length - 1);
                let day = result[2].label.substring(0, result[2].label.length - 1);
                let mData = m < 10 ? 0 + m + '月' : result[1].label;
                let dayData = day < 10 ? 0 + day + '日' : result[2].label;
                dayText.textContent = result[0].label + mData + dayData;
            },
            title: '多列选择器'
        });
    });
    // 省份
    const provincePicker = document.querySelector('#provincePicker');
    const pText = document.querySelector('#provinceText');
    //城市
    const cityPicker = document.querySelector('#cityPicker');
    const cityText = document.querySelector('#cityText');
    // 请求省份数据
    let prevHandleCityPick // 这个变量用来保存之前（上一次）的城市处理函数
    //遍历老数组
    $http.get('/address/province', function (res) {
        if (res.status === 0) {
            const pOption = res.data.map(function (item) {
                return {
                    label: item.name,
                    value: item.addressId
                }
            })
            // 绑定事件处理函数
            provincePicker.addEventListener('click', function () {
                weui.picker(pOption, {
                    onConfirm: function (result) {
                        pText.textContent = result[0].label
                        cityText.textContent = '未选择';
                        // 用省份id请求城市数据实现联动
                        $http.get(`/address/city/${result[0].value}`, function (res) {
                            if (res.status === 0) {
                                //判断数据是否未空
                                if (res.data.length) {
                                    const cOption = res.data.map(function (item) {
                                        return {
                                            label: item.name,
                                            value: item.addressId
                                        }
                                    })
                                    function cityPick() {
                                        weui.picker(cOption, {
                                            onConfirm: function (result) {
                                                cityText.textContent = result[0].label
                                            },
                                            title: '请选择城市'
                                        });
                                    }
                                    //解绑上一次城市事件
                                    cityPicker.removeEventListener('click', prevHandleCityPick);
                                    //每次绑定前存到变量
                                    prevHandleCityPick = cityPick;
                                    //绑定事件，配置城市选择器
                                    cityPicker.addEventListener('click', cityPick)
                                }
                            }
                        })

                    }
                });
            });
        }
    })
    //个性签名
    const txtLength = document.querySelector('.text_length');
    const weuiTextarea = document.querySelector('.weui-textarea');
    //判断长度仅限20
    weuiTextarea.addEventListener('keyup', function () {
        if (this.value.length > 20) {
            this.value = this.value.substr(0, 20)
        } else {
            txtLength.textContent = this.value.length;
        }
    })
    // 保存信息
    const keepData = document.querySelector('#keepData');
    // 获取页面填写的修改信息
    //id
    const user = JSON.parse(localStorage.getItem('user'));
    const userId = user.user.userId;
    //昵称
    const textName = document.querySelector('#js_input');
    //性别
    const textSex = document.querySelector('#sexText');
    //生日
    const textBirthday = document.querySelector('#dayText');
    //省份
    const textProvince = document.querySelector('#provinceText');
    //城市
    const textCity = document.querySelector('#cityText');
    //个性签名
    const textArea = document.querySelector('textarea');
    //个人信息获取
    $http.get(`/users/accountinfo?userId=${userId}`, function (res) {
        if (res.status === 0) {
            // 判断是否为空  不为空刷新自动附值
            if (res.data.nickname !== '') {
                textName.value = res.data.nickname;//昵称
            }
            if (res.data.gender !== '') {
                textSex.textContent = res.data.gender;//性别
            }
            const dayBirthday = res.data.birthday;//生日
            if (dayBirthday !== null) {
                //生日格式转换
                textBirthday.textContent = dayBirthday.substr(0, 10).substr(0, 4) + '年' + dayBirthday.substr(0, 10).substr(5, 2) + '月' + dayBirthday.substr(0, 10).substr(8, 2) + '日';
            }
            if (res.data.address !==null) {
                textProvince.textContent = res.data.address.split(',')[0];//省份
                textCity.textContent = res.data.address.split(',')[1];//城市
            }
            if(res.data.sign!==''){
                textArea.value = res.data.sign;//个性签名
            }
        }
    })
    //信息保存绑定事件
    keepData.addEventListener('click', function () {
        let str = textBirthday.textContent
        //请求个人信息
        // 拿到本地存储信息
        const userData = JSON.parse(localStorage.getItem('user'));
        // 获取ID
        const userId = userData.user.userId;
        //修改信息请求
        $http.post('/users/userEdit', {
            nickname: textName.value,//昵称
            gender: textSex.textContent,//性别
            birthday: str.substring(0, 4) + '-' + str.substring(5, 7) + '-' + str.substring(8, 10),//生日
            address: [textProvince.textContent, textCity.textContent],//省市区
            sign: textArea.value,//个性签名
            userId
        }, function (res) {
            if (res.status === 0) {
                // 获取个人信息
                $http.get(`/users/accountinfo?userId=${userId}`, function (res) {
                    if (res.status === 0) {
                        textName.value = res.data.nickname;//昵称
                        textSex.textContent = res.data.gender;//性别
                        const dayBirthday = res.data.birthday;//生日
                        //生日格式转换
                        textBirthday.textContent = dayBirthday.substr(0, 10).substr(0, 4) + '年' + dayBirthday.substr(0, 10).substr(5, 2) + '月' + dayBirthday.substr(0, 10).substr(8, 2) + '日';
                        textProvince.textContent = res.data.address.split(',')[0];//省份
                        textCity.textContent = res.data.address.split(',')[1];//城市
                        textArea.value = res.data.sign;//个性签名
                    }
                })
                window.utils.showToast(0, '信息修改成功');
            }
        })
    })
})